<?php

namespace app\admin\model;

use app\admin\validate\CateValidate;
use think\Model;
use think\model\concern\SoftDelete;

class Cate extends Model
{
    //软删除
    use SoftDelete;
    protected $deletetime ='delete_time';

    /***
     * cateadd增加数据
     * catedel删除数据
     * cateupd修改数据
     * cateselect查看数据
     */
    public function cateselect(){
        return Cate::select();
    }
    public function catefind($where){
        return Cate::where('id',$where)->find();

    }
    public function cateadd(array $data){
        $validate =new CateValidate();
        if(!$validate->check($data)){
            return $validate->getError();
        }
        if(!empty($data['mylogo'])){
            $dir ="./cateuploads/logo/".date('Ymd');
            if(!file_exists($dir)){
                mkdir($dir,0777,true);
            }
            //更改图片路径
            $truepath =str_replace('mylogo','logo',$data['mylogo']);
            //移动图片
            rename($data['mylogo'],$truepath);
            $data['mylogo'] =$truepath;
        }
        $ret =Cate::create($data);
        if($ret){
            return 1;
        }else{
            return 2;
        }
    }
    public function cateupd($data,$where){
        $validate =new CateValidate();
        if(!$validate->check($data)){
            return $validate->getError();
        }
        if(!empty($data['mylogo'])){
            $dir ="./cateuploads/logo/".date('Ymd');
            if(!file_exists($dir)){
                mkdir($dir,0777,true);
            }
            //更改图片路径
            $truepath =str_replace('mylogo','logo',$data['mylogo']);
            //移动图片
            rename($data['mylogo'],$truepath);
            $data['mylogo'] =$truepath;
        }
        $ret= Cate::where('id',$where)->update($data);
        if($ret){
            return 1;
        }else{
            return $ret;
        }
    }
    public function catedel($id){
        return Cate::destroy($id);
    }
}
